﻿using Bids.Core;
using Bids.Core.Infrastructure;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq.Expressions;
using Telerik.OpenAccess;
namespace Bids.Data
{
    public interface IDataContext : IDisposable
    {
        void Add(object instance);
        void Add(IEnumerable instances);
        void Delete(object instance);
        void Delete(IEnumerable instances);
        T GetObjectByKey<T>(ObjectKey objectKey);
        IQueryable<T> GetAll<T>();
        void SaveChanges();
        int ExecuteNonQuery(string commandText, params DbParameter[] parameters);
        int ExecuteNonQuery(string commandText, CommandType commandType, params DbParameter[] parameters);
        IList<T> ExecuteQuery<T>(string commandText, params DbParameter[] parameters);
        IList<T> ExecuteQuery<T>(string commandText, CommandType commandType, params DbParameter[] parameters);
        T ExecuteScalar<T>(string commandText, params DbParameter[] parameters);
        T ExecuteScalar<T>(string commandText, CommandType commandType, params DbParameter[] parameters);
        T[] ExecuteStoredProcedure<T>(string procedureName, IEnumerable<SqlParameter> parameterDefinition, params object[] parameterValues);
        T[] ExecuteStoredProcedure<T>(string procedureName, IEnumerable<SqlParameter> parameterDefinition, out IDictionary<string, object> outParameter, params object[] parameterValues);
    }
    /// <summary>
    /// 数据上下文
    /// </summary>
    public class DataContext : OpenAccessContext, IDataContext
    {
        static BidsFluentMetadataSource metadataSource = new BidsFluentMetadataSource();
        static BackendConfiguration backendConfiguration = new BackendConfiguration() { Backend = "mssql" };
        public DataContext(string dataConnection) : base(dataConnection, backendConfiguration, metadataSource){ }
    }
}